package com.icloud.system.boot.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.icloud.common.core.api.CommonResult;
import com.icloud.common.database.model.AdminUser;
import com.icloud.common.web.annotation.AdminOperationLog;
import com.icloud.common.web.annotation.Permission;
import com.icloud.common.web.enums.StateEnum;
import com.icloud.system.boot.dto.AddAdminUserDto;
import com.icloud.system.boot.query.AdminUserQuery;
import com.icloud.system.boot.service.AdminUserService;
import com.icloud.system.boot.vo.AdminUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "后台账户服务")
@RequestMapping("/admin/user")
@RequiredArgsConstructor
public class AdminUserController {
    private final AdminUserService adminUserService;

    @AdminOperationLog("添加账户")
    @ApiOperation("添加账户")
    @PostMapping("/add")
    //@Permission("system.adminUser.add")
    public CommonResult<Void> add(@Validated @RequestBody AddAdminUserDto dto){
        adminUserService.add(dto);
        return CommonResult.success();
    }

    @AdminOperationLog("编辑账户")
    @ApiOperation("编辑账户")
    @PostMapping("/edit")
    //@Permission("system.adminUser.edit")
    public CommonResult<Void> edit(@Validated @RequestBody AddAdminUserDto dto){
        adminUserService.edit(dto);
        return CommonResult.success();
    }

    @AdminOperationLog("封禁账户")
    @ApiOperation("封禁账户")
    @PutMapping("/banned/{id}")
    //@Permission("system.adminUser.banned")
    public CommonResult<Void> banned(@ApiParam("账户id") @PathVariable("id") Long id){
        adminUserService.update(new LambdaUpdateWrapper<AdminUser>()
                        .set(AdminUser::getState, StateEnum.STOP.getCode())
                .eq(AdminUser::getId,id));
        return CommonResult.success();
    }

    @AdminOperationLog("解除封禁账户")
    @ApiOperation("解除封禁账户")
    @PutMapping("/cancelBanned/{id}")
    //@Permission("system.adminUser.banned")
    public CommonResult<Void> cancelBanned(@ApiParam("账户id") @PathVariable("id") Long id){
        adminUserService.update(new LambdaUpdateWrapper<AdminUser>()
                .set(AdminUser::getState, StateEnum.NORMAL.getCode())
                .eq(AdminUser::getId,id));
        return CommonResult.success();
    }

    @ApiOperation("分页")
    @GetMapping("/page")
    //@Permission("system.adminUser.page")
    public CommonResult<IPage<AdminUserVo>> page(AdminUserQuery query){
        return CommonResult.success(adminUserService.page(query));
    }
}
